import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path : "/",
    redirect : "/home"
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    meta : {
      activeNum : 0,
      isShowtabbar : true
    },
    children : [
      {
        path: '/home/searchPopup',
        name: 'SearchPopup',
        component: () => import(/* webpackChunkName: "searchPopup" */ '../views/SearchPopup.vue')
      }
    ],
    
  },
  {
      path: '/topic',
      name: 'Topic',
      component: () => import(/* webpackChunkName: "Topic" */ '../views/Topic.vue'),
      meta : {
        activeNum : 1,
        isShowtabbar : true
      },
  },
  {
    path: '/category',
    name: 'Category',
    component: () => import(/* webpackChunkName: "Category" */ '../views/Category.vue'),
    meta : {
      activeNum : 2,
      isShowtabbar : true
    },
  },
  {
    path: '/cart',
    name: 'Cart',
    component: () => import(/* webpackChunkName: "Cart" */ '../views/Cart.vue'),
    meta : {
      activeNum : 3,
      isShowtabbar : true
    },
  },
  {
    path: '/user',
    name: 'User',
    component: () => import(/* webpackChunkName: "User" */ '../views/User.vue'),
    meta : {
      activeNum : 4,
      isShowtabbar : true
    },
  },
  {
    path: '/produceDeail',
    name: 'ProduceDeail',
    component: () => import(/* webpackChunkName: "ProduceDeail" */ '../views/ProduceDeail.vue'),
    meta :{
      isShowtabbar : false
    }
  },

  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,from,next)=>{
  let token = localStorage.getItem("token");
  if(to.path == "/cart"){
    if(token){
      next()
    }else{
      Vue.prototype.$toast("请先登录!")
      setTimeout(()=>{
        next("/user")
      },1000)
    }
    return
  }
  next()
})

export default router
